<template>
  <el-dialog
    :model-value="visible"
    title="供应商订单"
    width="600"
    :close-on-click-modal="false"
    @close="cls"
  >
    <el-form
      ref="formRel"
      :model="form"
      :rules="rules"
      label-width="120px"
      style="max-width: 800px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="采购单号">
            <el-input v-model="form.pcode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="对应客户订单号">
            <el-input v-model="form.code" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="供应商名称">
            <el-input v-model="form.name" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购费用">
            <el-input v-model="form.price" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="产品">
            <el-input v-model="form.product" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <div>
        <el-button type="primary" @click="savefrom">保存</el-button>
        <el-button type="default" @click="cls">取消</el-button>
      </div>

      <!-- Add other fields similarly -->
    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { getpcode, psave, updateStatus } from "@/api/order.js";

const form = ref({
  pcode: "",
  code: "",
  name: "",
  product: "",
  price: "",
  status: ""
});
const order = ref({
  code: "",
  status: ""
});

async function savefrom() {
  //验证规则 为false时，不会触发表单提交
  form.value.status = "1";
  let res = await psave(form.value);
  if (res.code == 200) {
    let res2 = await updateStatus(order.value);
    if (res2.code == 200) {
      ElMessage({
        message: "保存成功",
        type: "success"
      });
      emits("refresh");
      cls();
    }

    //刷新部门列表
    // emits("refresh");
    // cls();
  } else {
  }
}
const emits = defineEmits(["close", "refresh"]);

function cls() {
  emits("close");
}
async function init(code, product, status) {
  let pcodeget = await getpcode();
  form.value.pcode = pcodeget.data;
  form.value.code = code;
  form.value.product = product;
  order.value.code = code;
  order.value.status = status;
}
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
});

defineExpose({ init });
</script>
